package com.haoshuang.zhouzhoubang.ui.vm

import androidx.lifecycle.rxLifeScope
import com.haoshuang.zhouzhoubang.base.request
import com.haoshuang.zhouzhoubang.model.api.ApiRepo
import com.haoshuang.zhouzhoubang.model.bean.SfBean
import com.mufeng.libs.base.BaseViewModel
import com.mufeng.libs.net.netRequest
import com.mufeng.libs.state.StateData
import com.mufeng.libs.state.StatefulMutableLiveData

class InvitingMembersViewModel : BaseViewModel() {
    val getData2 = StatefulMutableLiveData<ArrayList<SfBean>>()
    fun getData2(vararg params: Pair<String, Any?>) {
        rxLifeScope.launch {
            request<ArrayList<SfBean>>("/api/Team/team_list", *params,
                onStart = {
                    getData2.value = StateData.Loading
                },
                onError = { code, msg ->
                    getData2.value = StateData.Error(code, msg)
                }, onSuccess = {
                    getData2.value = StateData.Success(it)
                })
        }
    }
    val getData = StatefulMutableLiveData<Any>()
        fun getData(vararg params: Pair<String, Any?>) {
            rxLifeScope.launch {
                request<Any>("/api/Team/invite_team_member", *params,
                    onStart = {
                        getData.value = StateData.Loading
                    },
                    onError = { code, msg ->
                        getData.value = StateData.Error(code, msg)
                    }, onSuccess = {
                        getData.value = StateData.Success(it)
                    })
            }
        }
    val getData1 = StatefulMutableLiveData<Any>()
    fun getData1(vararg params: Pair<String, Any?>) {
        rxLifeScope.launch {
            request<Any>("/api/Team/screening_phone", *params,
                onStart = {
                    getData1.value = StateData.Loading
                },
                onError = { code, msg ->
                    getData1.value = StateData.Error(code, msg)
                }, onSuccess = {
                    getData1.value = StateData.Success(it)
                })
        }
    }

    /**
     * 邀请不存在的账号
     */
    val inviteTeamMemberData = StatefulMutableLiveData<Any>()
    fun inviteTeamMember(phones: String, first_level_ids: String, second_level_ids: String, little_ids: String) = netRequest {
        start { inviteTeamMemberData.value = StateData.Loading }
        request { ApiRepo.inviteTeamMember(phones, first_level_ids, second_level_ids, little_ids)  }
        success { inviteTeamMemberData.value = StateData.Success(it.data) }
        error { code, msg ->
            inviteTeamMemberData.value = StateData.Error(code, msg)
        }
    }
}